package ch.elektroplan.mueller.passwordSave.business;

import java.io.File;
import java.util.HashMap;

import android.R.string;
import android.content.Context;
import android.widget.Toast;
import ch.elektroplan.mueller.passwordSave.business.supporters.FileLoader;

public class PasswordSafe {

	private String NAMEFILE = "/sdcard/passwordsafe/names.pws";
	private String PASSWORDS = "/sdcard/passwordsafe/passwords.pws";

	private FileLoader nameLoader, passwordLoader;
	private Context context;
	private String[] namesArr, wordsArr;
	
	public PasswordSafe(Context context) {

		this.nameLoader = new FileLoader(context);
		this.passwordLoader = new FileLoader(context);
		this.context = context;



		if (this.checkSDCard()) {

			try {
				nameLoader.searchFile(this.NAMEFILE);
				namesArr = nameLoader.getStringsArr();
				passwordLoader.searchFile(this.PASSWORDS);
				wordsArr = passwordLoader.getStringsArr();

			} catch (Exception e) {
			}

		} else {
			Toast.makeText(context, "some falure on the SD Card",
					Toast.LENGTH_SHORT).show();
		}
		
		

	}

	
	public String[] getNamesArr() {
		return namesArr;
	}


	public String[] getWordsArr() {
		return wordsArr;
	}


	private boolean checkSDCard() {
		File sd = new File("/sdcard/");
		File pwsFolder = new File("/sdcard/passwordsafe/");
		File names = new File(this.NAMEFILE);
		File word = new File(this.PASSWORDS);

		if (!(sd.isDirectory() && sd.exists() && sd.canRead() && sd.canWrite())) {
			return false;
		}
		if (!(pwsFolder.isDirectory() && pwsFolder.exists()
				&& pwsFolder.canRead() && pwsFolder.canWrite())) {
			return false;
		}
		if (!(names.exists() && names.isFile() && names.canRead())) {
			return false;
		}
		if (!(word.isFile() && word.exists() && word.canRead())) {
			return false;
		}

		//Toast.makeText(context, "sdcard ok", Toast.LENGTH_SHORT).show();
		return true;
	}
}
